package sgi.client;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

import sgi.message.Message;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.IOException;

public class SGIClientConnectUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField serveradd;
	private JTextField port;
	JTextArea notification;
	SGIClientConnectionController connection;

	/**
	 * Create the frame.
	 */
	public SGIClientConnectUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 302, 213);
		setTitle("SGI Client");
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblServerAddress = new JLabel("Server Address:");
		lblServerAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblServerAddress.setBounds(10, 29, 105, 26);
		contentPane.add(lblServerAddress);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPort.setBounds(10, 66, 51, 17);
		contentPane.add(lblPort);
		
		serveradd = new JTextField();
		serveradd.setBounds(118, 34, 124, 20);
		contentPane.add(serveradd);
		serveradd.setColumns(15);
		
		port = new JTextField();
		port.setBounds(118, 66, 124, 20);
		contentPane.add(port);
		port.setColumns(10);
		
		JButton btnConnect = new JButton("Connect");
		btnConnect.setBounds(118, 95, 89, 23);
		btnConnect.setToolTipText("Connect to server");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				 try {
					 	connection.startConnection(serveradd.getText(), Integer.valueOf(port.getText()));
				 } catch (IOException e) {setNotification("Connection to server failed."); }
			}
		});
		contentPane.add(btnConnect);
		
		notification = new JTextArea();
		notification.setForeground(new Color(255, 0, 0));
		notification.setBounds(21, 129, 239, 15);
		notification.setEditable(false);
		contentPane.add(notification);
	}
	
	public void setClient(SGIClientConnectionController connection)
	{
		this.connection = connection;
	}
	
	public void setNotification(String s)
	{
		notification.setText(s);
	}
	
	  public static void main(String[] args) throws IOException {
			
		  SGIClientConnectionController.createConnection();
	  }
}
	